#include "ChoseGoodsScene.h"
#include "ChoseGateScene.h"
#include "BattleGameMainScene.h"

using namespace cocos2d::ui;

bool ChoseGoodsScene::init() {
	if (!Layer::init()) {
		return false;
	}
	_node = CSLoader::createNode("eqiment/ChoseGoodsScene.csb");
	this->addChild(_node);

	this->initEntity();

	auto back_btn = dynamic_cast<Button*>(_node->getChildByName("back_btn"));
	auto confirm_btn = dynamic_cast<Button*>(_node->getChildByName("Confirm_btn"));
	if (back_btn == nullptr) {
		log("back button is null!");
	}
	else {
		back_btn->addClickEventListener([](Ref* ref) {
			Director::getInstance()->replaceScene(TransitionFlipY::create(0.3f,ChoseGateScene::createScene()));
		});
	}
	if (confirm_btn == nullptr) {
		log("confirm button is null!");
	}
	else {
		confirm_btn->addClickEventListener([this](Ref* ref) {
			_hero->retain();
			_drill->retain();
			Director::getInstance()->replaceScene(TransitionPageTurn::create(0.3f,BattleGameMainScene::createScene(_hero,_drill),false));
		});
	}

	return true;
}

void ChoseGoodsScene::initEntity() {
	this->_hero = Hero::create();
	this->_drill = Drill::create();

	auto hero_panel = _node->getChildByName("pv")->getChildByName("hero_panel");
	auto drill_panel = _node->getChildByName("pv")->getChildByName("drill_panel");
	_hero_body_sp =dynamic_cast<Sprite*>( hero_panel->getChildByName("body_sp"));
	_hero_head_sp = dynamic_cast<Sprite*>(hero_panel->getChildByName("head_sp"));
	_hero_leg_sp = dynamic_cast<Sprite*>(hero_panel->getChildByName("leg_sp"));
	_hero_hand_sp = dynamic_cast<Sprite*>(hero_panel->getChildByName("hand_sp"));
	_goods_sp1 = dynamic_cast<Sprite*>(hero_panel->getChildByName("goods_sp1"));
	_goods_sp2 = dynamic_cast<Sprite*>(hero_panel->getChildByName("goods_sp1"));
	_goods_sp2 = dynamic_cast<Sprite*>(hero_panel->getChildByName("goods_sp1"));
	_drill_attack_sp = dynamic_cast<Sprite*>(drill_panel->getChildByName("attack_sp"));
	_drill_body_sp = dynamic_cast<Sprite*>(drill_panel->getChildByName("body_sp"));
	_drill_head_sp = dynamic_cast<Sprite*>(drill_panel->getChildByName("head_sp"));
	_drill_light_sp = dynamic_cast<Sprite*>(drill_panel->getChildByName("light_sp"));
	dynamic_cast<Text*>(hero_panel->getChildByName("name_txt"))->setText(_hero->name);
	dynamic_cast<Text*>(hero_panel->getChildByName("life_txt"))->setText(StringUtils::format("%d",_hero->max_life));
	dynamic_cast<Text*>(hero_panel->getChildByName("blood_txt"))->setText(StringUtils::format("%d", _hero->max_blood));
	dynamic_cast<Text*>(hero_panel->getChildByName("speed_txt"))->setText(StringUtils::format("%d", _hero->moveSpeed));
	dynamic_cast<Text*>(drill_panel->getChildByName("life_txt"))->setText(StringUtils::format("%d", _drill->max_life));
	dynamic_cast<Text*>(drill_panel->getChildByName("blood_txt"))->setText(StringUtils::format("%d", _drill->max_blood));
	dynamic_cast<Text*>(drill_panel->getChildByName("speed_txt"))->setText(StringUtils::format("%d", _drill->moveSpeed));
	auto hero_skill_txt = dynamic_cast<Text*>(hero_panel->getChildByName("skill_sv")->getChildByName("skill_txt"));
	auto drill_skill_txt = dynamic_cast<Text*>(drill_panel->getChildByName("skill_sv")->getChildByName("skill_txt"));
	std::string skill=hero_skill_txt->getString();
	/*for (int i = 0; i < _hero->skill_att->size(); i++) {
		skill.append(StringUtils::format("%8s\t",_hero->skill_att[i][0]));
		skill.append(StringUtils::format("%3d\t", _hero->skill_att[i][1]));
		skill.append(StringUtils::format("%3d\n", _hero->skill_num[i][0]));
	}*/
	for (std::map<std::string, int>::iterator it = _hero->skill_att->begin(); it != _hero->skill_att->end(); it++) {
		skill.append(StringUtils::format("%-10s",it->first.c_str()));
		skill.append(StringUtils::format("%d", it->second));
		skill.append(StringUtils::format("%4d\n", _hero->skill_num->at(it->first)));
	}
	hero_skill_txt->setText(skill);

	skill = drill_skill_txt->getString();
	for (std::map<std::string, int>::iterator it = _drill->skill_att->begin(); it != _drill->skill_att->end(); it++) {
		skill.append(StringUtils::format("%-10s", it->first.c_str()));
		skill.append(StringUtils::format("%d", it->second));
		skill.append(StringUtils::format("%4d\n", _drill->skill_num->at(it->first)));
	}
	drill_skill_txt->setText(skill);
}

Scene* ChoseGoodsScene::createScene() {
	auto scene = Scene::create();
	auto layer = ChoseGoodsScene::create();
	scene->addChild(layer);
	return scene;
}